package com.heima.behavior.service.impl;

import com.heima.behavior.dtos.ReadBehaviorDto;
import com.heima.behavior.pojos.ApReadBehavior;
import com.heima.behavior.service.ApReadBehaviorService;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.thread.ApTheadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author shitou
 * @className ApReadBehaviorServiceImpl
 * @description: TODO
 */
@Service
@Slf4j
public class ApReadBehaviorServiceImpl implements ApReadBehaviorService {

    @Resource
    private MongoTemplate mongoTemplate;

    /**
     * 记录阅读行为
     *
     * @param dto
     * @return
     * @deprecated 阅读行为直接存入mongo
     */
    @Override
    public ResponseResult readBehavior(ReadBehaviorDto dto) {
        log.info(" 记录阅读行为已经执行");

        // 参数校验
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        // 用户未登录
        ApUser apUser = ApTheadLocalUtil.getApUser();
        if (apUser == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        Integer userId = apUser.getId();
        // 游客需要进行登录，才能进行用户行为
        if (userId == 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }

        // 校验阅读与文章id参数
        Short count = dto.getCount();
        Long dtoArticleId = dto.getArticleId();
        if (count != 1 || dtoArticleId == null || dtoArticleId < 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        // 先查询当前文章阅读数量,用户阅读就count加1
        Long articleId = dto.getArticleId();
        Query query = Query.query(Criteria.where("articleId").is(articleId).and("userId").is(userId));
        ApReadBehavior readBehavior = mongoTemplate.findOne(query, ApReadBehavior.class);
        // 文章不存在 保存关注数据到数据库
        if (readBehavior == null) {
            //2.3.2 保存关注数据到数据库
            readBehavior = new ApReadBehavior();
            readBehavior.setReadDuration(dto.getReadDuration());
            readBehavior.setUpdatedTime(new Date());
            readBehavior.setArticleId(dto.getArticleId());
            readBehavior.setCount(1);
            readBehavior.setCreatedTime(new Date());
            readBehavior.setLoadDuration(dto.getLoadDuration());
            readBehavior.setPercentage(dto.getPercentage());
            readBehavior.setUserId(userId.longValue());
            readBehavior.setEquipmentId(dto.getEquipmentId());
            mongoTemplate.insert(readBehavior);
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }

        // 封装阅读数据，修改mongo原来数据

        readBehavior.setCount(readBehavior.getCount() + count);
        readBehavior.setReadDuration(dto.getReadDuration());
        readBehavior.setArticleId(dtoArticleId);
        readBehavior.setId(readBehavior.getId());
        // 存入mongo
        mongoTemplate.save(readBehavior);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
